package com.cn.controller.user;

import com.cn.entity.User;
import com.cn.service.IdleItemService;
import com.cn.vo.PageResult;
import com.cn.vo.Result;
import com.cn.entity.IdleItem;
import com.cn.mapper.IdleItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户端商品控制器
 */
@RestController
@RequestMapping("user/item")
public class UserItemController {

    @Autowired
    private IdleItemService idleItemService;
    
    @Autowired
    private IdleItemMapper idleItemMapper;

    /**
     * 获取商品列表（已发布状态）
     */
    @GetMapping("list")
    public Result getItemList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "nums", defaultValue = "8") Integer nums,
                              @RequestParam(value = "findValue", defaultValue = "") String findValue) {
        PageResult<IdleItem> pageResult = idleItemService.findIdleItem(findValue, page, nums);
        return Result.success(pageResult);
    }

    /**
     * 根据分类获取商品列表
     */
    @GetMapping("listByLabel")
    public Result getItemByLabel(@RequestParam("idleLabel") Integer idleLabel,
                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "nums", defaultValue = "8") Integer nums) {
        PageResult<IdleItem> pageResult = idleItemService.findIdleItemByLabel(idleLabel, page, nums);
        return Result.success(pageResult);
    }
    
    /**
     * 获取我发布的商品
     */
    @GetMapping("myPublished")
    public Result getMyPublished(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.success(new java.util.ArrayList<>());
        }
        
        List<IdleItem> list = idleItemMapper.getAllIdleItem(user.getId());
        return Result.success(list);
    }
}
